package com.zhong.gang.app.ui.waidiao.input;

import android.content.Intent;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.zhong.gang.app.R;
import com.zhong.gang.app.api.Api;
import com.zhong.gang.app.databinding.ActivityWaiDiaoBinding;
import com.zhong.gang.app.db.UserBean;
import com.zhong.gang.app.db.UserUtils;
import com.zhong.gang.app.ui.address.AddressBean;
import com.zhong.gang.app.ui.carorder.OrderNumBean;
import com.zhong.gang.app.ui.waidiao.input.controller.WaiDiaoDataPresenter;
import com.zhong.gang.app.utils.WaiDiaoCarInputView;
import com.zhong.gang.applib.base.act.ActBase;
import com.zhong.gang.applib.config.Config;
import com.zhong.gang.applib.controller.ImgController;
import com.zhong.gang.applib.ui.adapter.AddImgAdapter;
import com.zhong.gang.applib.ui.matching.MatchingAct;
import com.zhong.gang.applib.ui.matching.MatchingBean;
import com.zhong.gang.applib.ui.view.ViewClickedListener;

public class WaiDiaoAct extends ActBase<ActivityWaiDiaoBinding> implements WaiDiaoCarInputView.CarViewListener, ViewClickedListener {
    private WaiDiaoDataPresenter presenter;
    private AddImgAdapter clAdapter,hwAdapter;
    private ImgController imgController;
    @Override
    public int LayoutRes() {
        return R.layout.activity_wai_diao;
    }

    @NonNull
    @Override
    public String CreateTitle() {
        return "新建外调货业";
    }

    //公司  车牌  送货地点
    //货主派车 车牌 送货地点

    //送货地点     装货料型 毛重 皮重 净重
    //打款放车     毛重 皮重 扣渣  装货廖鑫1  廖鑫2 结算金额
    @Override
    public void initView() {
        bind.setAct(this);
        presenter = new WaiDiaoDataPresenter(this);

        imgController = new ImgController();
        clAdapter = imgController.initImgAdapter(this,bind.allRecyclerView, Config.ALL_CLICKED);
        hwAdapter = imgController.initImgAdapter(this,bind.halfRecyclerView,Config.HALF_CLICKED);
        imgController.setNeedImg(clAdapter,this,true, Config.ALL_CLICKED,true);
        imgController.setNeedImg(hwAdapter,this,true, Config.HALF_CLICKED,true);
        imgController.addImgView(clAdapter,"",true);
        imgController.addImgView(hwAdapter,"",true);

        bind.carView.setListener(this);
        bind.shipperView.setListener(this);
    }

    public void onViewClicked(View view){
        switch (view.getId()){
            case R.id.sure:
                presenter.upData(imgController.getFile(clAdapter)
                        ,imgController.getFile(hwAdapter)
                        ,bind.carView.getAddressId(),bind.shipperView.getBean()
                        ,bind.payWay.getType(),bind.payWay.getMWeight(),bind.payWay.getPWeight(),bind.payWay.getKWeight()
                        ,bind.carView.getCarType()
                        ,bind.carView.getCarNum(),bind.carView.getOrderNumberId(),bind.payWay.getJsonArray(),bind.payWay.getUpMoneny());
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode != RESULT_OK ||data == null )
            return;
        imgController.onActivityResult(requestCode == Config.ALL_CLICKED?clAdapter:hwAdapter,resultCode,data);
        if (requestCode == Config.USER)
            bind.shipperView.setData((MatchingBean) data.getSerializableExtra(Config.USER_IFNO));
    }

    @Override
    public void onViewClicked() {
        UserBean userBean = UserUtils.getIntent(this).getUser();
        MatchingAct.openAct(this, Api.getConsignorinfo
                ,userBean.isLogin(),userBean.getToken(),userBean.getId()+""
                ,UserUtils.getIntent(this).getRoleId()+"",true,false, Config.USER,false);
    }

    @Override
    public void onAddress(AddressBean bean) {
        if (bean == null){
            bind.payWay.setSteelMillId(-1,"",0.0);
            return;
        }
        bind.payWay.setSteelMillId(bean.getId(),bean.getName(),0.0);
    }

    @Override
    public void onAddress(OrderNumBean bean) {
        bind.payWay.setSteelMillId(bean.getEndLocalId(),bean.getEndLocal(),bean.getPrice());
    }
}
